package com.sjriley.zappit.services;
import com.sjriley.zappit.PreferencesActivity;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
public class LocationService extends IntentService
{
public final static String NAME = "locationService";
private static final String TAG = LocationService.class.getSimpleName();
private LocationManager locationManager;
public LocationService()
{
super(NAME);
}
@Override
protected void onHandleIntent(Intent intent)
{
LocationListener gpsListener = new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2){
Log.d(TAG, "STATUS CHANGE");
}
@Override
public void onProviderEnabled(String arg0) {
Log.d(TAG, "ENABLED");
}
@Override
public void onProviderDisabled(String arg0) {
Log.d(TAG, "DISABLED");
}
@Override
public void onLocationChanged(Location location){
updateLocation(location);
}
};
LocationListener networkListener = new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2){
Log.d(TAG, "STATUS CHANGE");
}
@Override
public void onProviderEnabled(String arg0) {
Log.d(TAG, "ENABLED");
}
@Override
public void onProviderDisabled(String arg0) {
Log.d(TAG, "DISABLED");
}
@Override
public void onLocationChanged(Location location){
Log.d(TAG, "NETWORK LOCATION CHANGED");
updateLocation(location);
}
};
Log.d(TAG, "STARTING LISTENING");
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
for (String string : locationManager.getProviders(true))
{
Log.d(TAG, "Provider:" + string);
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, networkListener);
clearLocation();
int timeCount = 0;
int locationCount = 0;
while (true) {
synchronized (this)
{
timeCount++;
Log.d(TAG, "STILL WAITING FOR LOCATION");
try
{
wait(2000);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d(TAG, "GPS found");
}
else {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
Log.d(TAG, "Network found");
}
}
//after 8 minutes give up as this will kill the battery
if (location != null) {
Log.d(TAG, "Found location no:" + locationCount);
locationCount++;
}
if (locationCount > 2 || timeCount > 10) {
updateLocation(location);
locationManager.removeUpdates(gpsListener);
locationManager.removeUpdates(networkListener);
stopSelf();
break;
}
}
catch (NullPointerException e){
// Log.d(TAG, "Something null");
}
catch (InterruptedException e)
{
}
}
}
}
private void updateLocation(Location location) {
SharedPreferences prefs = getSharedPreferences(PreferencesActivity.DEFAULT_PREFERECES, 0);
SharedPreferences.Editor editor = prefs.edit();
Log.d(TAG, "FOUND LAT: "+ location.getLatitude());
Log.d(TAG, "FOUND LONG: "+ location.getLongitude());
editor.putString(PreferencesActivity.LATITUDE, (Double.toString(location.getLatitude())));
editor.putString(PreferencesActivity.LONGITUDE, (Double.toString(location.getLongitude())));
editor.commit();
}
private void clearLocation() {
SharedPreferences prefs = getSharedPreferences(PreferencesActivity.DEFAULT_PREFERECES, 0);
SharedPreferences.Editor editor = prefs.edit();
Log.d(TAG, "clearing location");
//editor.putString(PreferencesActivity.LATITUDE, (Double.toString(location.getLatitude())));
// editor.putString(PreferencesActivity.LONGITUDE, (Double.toString(location.getLongitude())));
editor.commit();
}
}